package com.core.jdbc;

import com.core.comment.StringUtil;
import com.core.jdbc.annotate.MyId;
import com.core.jdbc.annotate.MyTable;
import com.testAll.Cust;
import sun.applet.Main;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SqlBase {

    public static <T> Object add(T t) throws SQLException {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Object id = null;
        try {
            // 获得连接:
            Connection conn = SqlHelper.getConnect();
            conn.setAutoCommit(true);
            // 编写SQL：
            Map<String, Object> par = new HashMap<>();
            String sql = doSqlPar(t, par);
            sql = SqlUtil.parseSql(sql, par);
            System.out.println("处理后的sql:" + sql);
            pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            // 执行sql:
            pstmt.executeUpdate();
            rs = pstmt.getGeneratedKeys();

            if (rs.next()) {
                id = rs.getObject(1);

            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (pstmt != null) {
                pstmt.close();
            }
            if (con != null) {
                con.close();
            }
        }
        return id;
    }


    public static String doSqlPar(Object obj, Map<String, Object> par) {
        List<String> names = new ArrayList<>();
        List<String> namesP = new ArrayList<>();
        try {
            String idName = getIdName(obj.getClass());
            if (StringUtil.isBlack(idName)) {
                throw new MySqlException(obj.getClass().getSimpleName() + "没有主键注解");
            }
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();

                // 过滤class属性
                if (key != null && !"class".equals(key) && !key.equals(idName)) {
                    names.add(key);
                    namesP.add(":" + key);
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);
                    par.put(key, value);

                }
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (ReflectiveOperationException e) {
            e.printStackTrace();
        }
        String table = getTableName(obj.getClass());
        if (StringUtil.isBlack(table)) {
            throw new MySqlException(obj.getClass().getSimpleName() + "的表名没有注解");
        }
        String sql = "insert into " + getTableName(obj.getClass()) + " (" + StringUtil.join(names) + ") values (" + StringUtil.join(namesP) + ")";
        return sql;
    }

    public static String getIdName(Class<?> cclazz) {
        Field[] fields = cclazz.getDeclaredFields();
        for (Field field : fields) {
            MyId myid = field.getAnnotation(MyId.class);
            if (myid != null) {
                return field.getName();
            }
        }
        return null;
    }

    public static String getTableName(Class<?> clazz) {
        MyTable myTable = clazz.getAnnotation(MyTable.class);
        if (myTable != null) {
            return myTable.value();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(getIdName(Cust.class));
    }
}
